import {ajax} from "../../utils/httpRequest.js";

import {URI_ADDORUPDATEADDRESS} from "../../api.js";
import {URI_ADDRESSLIST} from "../../api";

Page({
  data: {
    region: [],
    userName: '',
    telNumber: '',
    addressInfo: '',
    isDefaultAddress: 1,
    addressOneId: '',
    checked: true
  },
  changeRegin(e) {

    this.setData({
      region: e.detail.value
    });

  },
  swChange(e) {
    console.log(e.detail.value);
    if (e.detail.value) {
      this.setData({
        checked: true
      })
    } else {
      this.setData({
        checked: false
      })
    }
  },
  onLoad: function (options) {
    console.log(options);
    if (options.id) {
      this.setData({
        addressOneId: options.id
      });
      this.getAddress_in_id(options.id);
    }
  },
  getAddress_in_id(id) {
    ajax.get(URI_ADDRESSLIST, {
      addressId: id
    }, {}, res => {
      let addressOne = res.data.data[0];
      console.log(addressOne);
      this.setData({
        userName: addressOne.userName,
        telNumber: addressOne.phone,
        addressInfo: addressOne.address,
        checked: addressOne.isDefaultAddress == 1 ? true : false,
        region: [addressOne.province, addressOne.city, addressOne.district]
      });
      // this.setData({
      //   address_list:res.data.data
      // });
    });
  },
  getWechatAddress() {
    wx.chooseAddress({
      success: (res) => {
        this.setData({
          region: [res.provinceName, res.cityName, res.countyName],
          userName: res.userName,
          telNumber: res.telNumber,
          addressInfo: res.detailInfo,
        });
        // console.log(res);
        // console.log(res.userName)
        // console.log(res.postalCode)
        // console.log(res.provinceName)
        // console.log(res.cityName)
        // console.log(res.countyName)
        // console.log(res.detailInfo)
        // console.log(res.nationalCode)
        // console.log(res.telNumber)
      }
    })
  },
  savePersonInfo: function (e) {
    var data = e.detail.value;
    var telRule = /^1[3|4|5|7|8]\d{9}$/, nameRule = /^[\u2E80-\u9FFF]+$/;
    if (data.name == '') {
      this.showMessage('请输入姓名')
    } else if (!nameRule.test(data.name)) {
      this.showMessage('请输入中文名')
    } else if (data.tel == '') {
      this.showMessage('请输入手机号码')
    } else if (!telRule.test(data.tel)) {
      this.showMessage('手机号码格式不正确')
    } else if (data.province == '') {
      this.showMessage('请选择所在地区')
    } else if (data.city == '') {
      this.showMessage('请选择所在地区')
    } else if (data.district == '') {
      this.showMessage('请选择所在地区')
    } else if (data.address == '') {
      this.showMessage('请输入详细地址')
    } else {
      ajax.post(URI_ADDORUPDATEADDRESS, {
        id: this.data.addressOneId,
        userName: data.name,
        phone: data.tel,
        isDefaultAddress: this.data.checked ? 1 : 0,
        province: data.province,
        city: data.city,
        district: data.district,
        address: data.address
      }, {}, res => {
        wx.showModal({
          title: "提示",
          content: res.data.message,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack();
            }
          }
        });
      })
    }
  },
  showMessage: function (text) {
    wx.showToast({
      title: text,
      image: "/images/icon-warning.png",
    });
  }
});